package fun.linze.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import fun.linze.domain.TWallpaper;
import fun.linze.service.TWallpaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/wallpaper")
public class WallpaperController {
    @Autowired
    private TWallpaperService wallpaperService;

    /// 获取壁纸列表（分页）
    @GetMapping
    public ResponseEntity<Page<TWallpaper>> getWallpapers(@RequestParam(defaultValue = "1") long current, @RequestParam(defaultValue = "10") long size) {
        Page<TWallpaper> page = new Page<>(current, size);
        return ResponseEntity.ok(wallpaperService.page(page));
    }


    ///获取壁纸详情
    @GetMapping("/{id}")
    public ResponseEntity<TWallpaper> getWallpaperById(@PathVariable Long id) {
        // 使用UpdateWrapper原子性地增加浏览量
        boolean updated = wallpaperService.update(new UpdateWrapper<TWallpaper>().eq("id", id).setSql("view_count = view_count + 1"));
        if (updated) {
            // 然后获取壁纸信息
            return ResponseEntity.ok(wallpaperService.getById(id));
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    /// 添加壁纸
    @PostMapping
    public ResponseEntity<TWallpaper> addWallpaper(@RequestBody TWallpaper wallpaper) {
        //TODO:连接到文件处理

        wallpaperService.save(wallpaper);
        return ResponseEntity.status(HttpStatus.CREATED).body(wallpaper);
    }
    ///删除壁纸
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteWallpaper(@PathVariable Long id) {
        //TODO:连接到文件处理
        wallpaperService.removeById(id);
        return ResponseEntity.noContent().build();
    }

    /// 更新壁纸信息
    @PutMapping("/{id}")
    public ResponseEntity<TWallpaper> updateWallpaper(@PathVariable Long id, @RequestBody TWallpaper wallpaper) {
        wallpaper.setId(id);
        boolean updated = wallpaperService.updateById(wallpaper);
        if (updated) {
            return ResponseEntity.ok(wallpaper);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /// 点赞壁纸
    @PostMapping("/{id}/like")
    public ResponseEntity<Void> likeWallpaper(@PathVariable Long id) {
        TWallpaper wallpaper = wallpaperService.getById(id);
        if (wallpaper != null) {
            wallpaper.setLikeCount(wallpaper.getLikeCount() + 1);
            wallpaperService.updateById(wallpaper);
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /// 下载壁纸
    @GetMapping("/{id}/download")
    public ResponseEntity<String> downloadWallpaper(@PathVariable Long id) {
        TWallpaper wallpaper = wallpaperService.getById(id);
        if (wallpaper != null) {
            wallpaper.setDownloadCount(wallpaper.getDownloadCount() + 1);
            wallpaperService.updateById(wallpaper);
            return ResponseEntity.ok(wallpaper.getFileUrl());
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /// 更新壁纸状态
    @PutMapping("/{id}/status")
    public ResponseEntity<Void> updateWallpaperStatus(@PathVariable Long id, @RequestParam Integer status) {
        TWallpaper wallpaper = wallpaperService.getById(id);
        if (wallpaper != null) {
            wallpaper.setStatus(status);
            wallpaperService.updateById(wallpaper);
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
